package pyf.demo08;

/**
 * @author pengyifeng
 * @version 5.1
 * @ClassName ThreadStateDemo.java
 * @Description 线程状态
 * @createTime 2021年07月27日 09:57:00
 */
public class ThreadStateDemo{
    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("线程运行");
        });

        //开始观察线程状态
        Thread.State state = thread.getState();
        System.out.println("启动前:"+state);

        //启动后
        thread.start();
        state = thread.getState();
        System.out.println("启动后:"+state);

        //只要线程不结束就一直运行
        while (!thread.getState().equals(Thread.State.TERMINATED)){
            Thread.sleep(100);
            //更新状态
            System.out.println(thread.getState());
        }
    }

}
